/*
 * main.c

 *
 *  Created on: Nov 19, 2013
 *      Author: adijoshi
 */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cache_N.h"

void exec_Traces(char *inputFileName);

int main(int argc,char* argv[])
{
	init_Cache();
	
	if (argc != 2)// display error message about missing file name
		printf("\nPlease include file name as an argument.\n");
	else
		exec_Traces(argv[1]);
	return 0;
}

void exec_Traces(char *inputFileName)
{
	unsigned int accessType;
	unsigned int address;
	FILE *input_file_ptr;
	
	input_file_ptr = fopen(inputFileName, "r");

	if (input_file_ptr == NULL)
	{
		printf("Error while opening the file.\n");
	}
	else
	{
		while (fscanf(input_file_ptr, "%d", &accessType) != EOF)
		{
			if (accessType >= 7)
				address = 0;
			else
				fscanf(input_file_ptr, "%x", &address);

			switch (accessType){
			case CPU_READ_D:
				exec_Read_Req(CPU_READ_D, address);
				break;
			case CPU_READ_I:
				exec_Read_Req(CPU_READ_I, address);
				break;
			case CPU_WRITE_D:
				exec_Write_Req(CPU_WRITE_D, address);
				break;
			case SNOOPED_INVALIDATE:
				put_Snoop_Result('I', address);
				break;
			case SNOOPED_READ:
				put_Snoop_Result('R', address);
				break;
			case SNOOPED_WRITE:
				put_Snoop_Result('W', address);
				break;
			case SNOOPED_RFO:
				put_Snoop_Result('M', address);
				break;
			case CLEAR_CACHE:
				clear_Cache_Contents();
				break;
			case PRINT_CACHE:
				printf("\n\n------------- CONTENTS AND STATE OF EACH VALID CACHE LINE ---------------\n\n");
				print_Cache_Contents();
				printf("\n\n----------------------------------------------------------------------\n");
				break;
			default:
				printf("\nEncountered n = %d : unused access type\n", accessType);
				break;
			}
		}
		fclose(input_file_ptr);

		print_Cache_Stats();
	}
	getchar();
}
